import {
  myFetch
} from '@/config/apiServe/fetch-config.js'
export default {
  data() {
    return {
      total: 0,
      nodes: [],
      range: {
        max: 0,
        min: 0
      },
      nowTime: ''
    }
  },
  mounted() {
    this.times()
    this.getNodes()
  },
  methods: {
    times() {
      let date = new Date();
      let Year = date.getFullYear();
      let Month = date.getMonth() + 1;
      let Day = date.getDate();
      let Hour = date.getHours()
      let Minutes = date.getUTCMinutes()
      let second = date.getUTCSeconds()
      this.nowTime = `截至北京时间${Year}年${Month}月${Day}日 ${Hour}:${Minutes}:${second}`;
    },
    getNodes() {
      let data = {
        "id": 1223,
        "method": "NetScan.PeersLocation",
        "params": []
      }
      myFetch(data, '/map').then(res => {
        this.netScanRespHandleBase(res)
        this.range.max = this.nodes[0].value;
        this.range.min = this.nodes[this.nodes.length - 1].value;
      })
    },
    netScanRespHandleBase: function (data) {
      if (!data.result.countries) {
        return
      }
      let len = data.result.countries.length
      for (let i = 0; i < len; i++) {
        let item = data.result.countries[i];
        if (item.country !== 'XX') {
          this.total = this.total + item.value;
        }
      }
      data.result.countries.forEach(item => {
        if (item.country !== 'XX') {
          let percent = (item.value * 100 / this.total).toFixed(2) + '%';
          let place = '';
          switch (item.country) {
            case "中国":
              place = "中国大陆";
              break;
            case "香港":
            case "台湾":
            case "澳门":
              place = "中国" + item.country;
              break;
            default:
              place = item.country;
              break;
          }
          this.nodes.push({
            name: place,
            value: item.value,
            percent: percent,
          })
        }
      });
      let compare = function (property) {
        return function (obj1, obj2) {
          let value1 = obj1[property];
          let value2 = obj2[property];
          return value2 - value1; // 降序
        }
      };
      this.nodes = this.nodes.sort(compare('value'));
    }
  }
}